// XComboBox.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "XComboBox.h"


// CXComboBox

IMPLEMENT_DYNAMIC(CXComboBox, CComboBox)

CXComboBox::CXComboBox()
{
    mb_drop_down_only = false;
}

CXComboBox::~CXComboBox()
{
}


BEGIN_MESSAGE_MAP(CXComboBox, CComboBox)
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////
int CXComboBox::AddString(LPCTSTR str, DWORD_PTR data, COLORREF col)
{
    int idx = CComboBox::AddString(str);
    SetItemData(idx,data);
    if (madw_colors.GetCount()<=idx) madw_colors.SetSize(idx+1);

    if (col==-1) col = GetSysColor(COLOR_WINDOWTEXT);
    madw_colors[idx]=col;

    return idx;
}


//////////////////////////////////////////////////////////////////////////
void CXComboBox::SetColor(int idx, COLORREF col /*= -1*/)
{
    if (col==-1) col = GetSysColor(COLOR_WINDOWTEXT);
    madw_colors[idx] = col;

    if (idx == GetCurSel())
        Invalidate();
}


//////////////////////////////////////////////////////////////////////////
int CXComboBox::Select(DWORD_PTR data)
{
    for (int t=0;t<GetCount();t++)
        if (GetItemData(t)== data)
        {
            SetCurSel(t);
            return t;
        }

    SetCurSel(0);
    return -1;
}


//////////////////////////////////////////////////////////////////////////
void CXComboBox::ResetContent()
{
    CComboBox::ResetContent();
    madw_colors.RemoveAll();
}


//////////////////////////////////////////////////////////////////////////
void CXComboBox::PreSubclassWindow()
{
    CComboBox::PreSubclassWindow();

    ASSERT(GetStyle() & CBS_OWNERDRAWFIXED);     // Assert Proper Style Set
}


//////////////////////////////////////////////////////////////////////////
void CXComboBox::MeasureItem(LPMEASUREITEMSTRUCT lp)
{
    ::SendNotifyMessage(GetSafeHwnd(),WM_MEASUREITEM,0,(LPARAM)lp);
}




//////////////////////////////////////////////////////////////////////////
void CXComboBox::SetDropList(bool en_droplist)
{
    if (mb_drop_down_only == en_droplist) return;
    mb_drop_down_only = en_droplist;


    // M.Jurkait [10/6/2006]
    // -------------------------
    // recreate control
    // -------------------------
    ASSERT(GetSafeHwnd());

    // get current stats
    CWnd* pParent = GetParent();
    ASSERT(pParent);
    if (pParent == NULL) return;

    // get current attributes
    DWORD dwStyle = GetStyle();
    if (en_droplist) dwStyle = (dwStyle&(~(CBS_DROPDOWN|CBS_DROPDOWNLIST))) | CBS_DROPDOWNLIST;
    else    dwStyle = (dwStyle&(~(CBS_DROPDOWN|CBS_DROPDOWNLIST))) | CBS_DROPDOWN;

    DWORD dwStyleEx = GetExStyle();
    CRect rc;
    GetDroppedControlRect(&rc);
    pParent->ScreenToClient(&rc); // map to client co-ords
    UINT nID = GetDlgCtrlID();
    CFont* pFont = GetFont();
    CWnd* pWndAfter = GetNextWindow(GW_HWNDPREV);

    // get the currently selected text (and whether it is a valid list selection)
    CString sCurText;
    int nCurSel = GetCurSel();
    BOOL bItemSelValid = nCurSel != -1;
    if (bItemSelValid)  GetLBText(nCurSel, sCurText);
    else     GetWindowText(sCurText);

    // copy the combo box items into a temp combobox (not sorted)
    // along with each item's data
    CXComboBox comboNew;
    if (!comboNew.CreateEx(dwStyleEx, _T("COMBOBOX"), _T(""), dwStyle, rc, pParent, nID, NULL)) return;
    comboNew.SetFont(pFont);
    int nNumItems = GetCount();
    for (int n = 0; n < nNumItems; n++)
    {
        CString sText;
        GetLBText(n, sText);
        int nNewIndex = comboNew.AddString(sText,GetItemData(n));
    }

    // re-set selected text
    if (bItemSelValid)
        comboNew.SetCurSel(comboNew.FindStringExact(-1, sCurText));
    else
        comboNew.SetWindowText(sCurText);

    // destroy the existing window, then attach the new one
    DestroyWindow();
    HWND hwnd = comboNew.Detach();
    Attach(hwnd);

    // position correctly in z-order
    SetWindowPos(pWndAfter == NULL ? &CWnd::wndBottom : pWndAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}



//////////////////////////////////////////////////////////////////////////
void CXComboBox::DrawItem(LPDRAWITEMSTRUCT pDIStruct)
{
    COLORREF crColor = 0;
    CString  cColor;    // Color Name Buffer
    CRect  rItemRect(pDIStruct -> rcItem);
    CDC   dcContext;
    int   iItem = pDIStruct -> itemID;
    int   iState = pDIStruct -> itemState;

    if (!dcContext.Attach(pDIStruct -> hDC))  return;


    // Erase background
    if (iState & ODS_SELECTED)
        dcContext.FillSolidRect(&rItemRect, GetSysColor(COLOR_HIGHLIGHT));
    else
        dcContext.FillSolidRect(&rItemRect, GetSysColor(COLOR_WINDOW));


    // Draw Focus Rect
    if (iState & ODS_FOCUS)
        dcContext.DrawFocusRect(&rItemRect);


    // Draw Color Text And Block... If Not An Empty Item
    if (iItem != -1)
    {
        GetLBText(iItem, cColor);    // Get Color Text


        if (iState & ODS_DISABLED)
        {
            dcContext.SetTextColor(GetSysColor(COLOR_GRAYTEXT));
        }
        else
            if (iState & ODS_SELECTED)
            {
                dcContext.SetTextColor((0x00FFFFFF & ~(madw_colors[iItem])));
            }
            else
            {
                dcContext.SetTextColor(madw_colors[iItem]);
            }

        dcContext.SetBkMode(TRANSPARENT);
        dcContext.TextOut(rItemRect.left, rItemRect.top, cColor);
    }


    dcContext.Detach();

    return;
}


